#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QMessageBox>
#include<QDesktopServices>
#include<QUrl>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化默认选择
    ui->rb0_19->setChecked(true);
    ui->rbGood->setChecked(true);
    ui->rbMan->setChecked(true);
    //添加性别虚拟分组
    m_GenderGroup = new QButtonGroup;
    m_GenderGroup->addButton(ui->rbMan,0);
    m_GenderGroup->addButton(ui->rbWoman,1);
    //添加状态分组
    m_StatusGroup = new QButtonGroup;
    m_StatusGroup->addButton(ui->rbGood,0);
    m_StatusGroup->addButton(ui->rbMid,1);
    m_StatusGroup->addButton(ui->rbBad,2);

    //关联两个分组信号和槽
    connect(m_GenderGroup,SIGNAL(buttonClicked(int)),this,SLOT(RecvGenderID(int)));
    connect(m_StatusGroup,SIGNAL(buttonClicked(int)),this,SLOT(RecvStatusID(int)));

     //设置三态模式
    ui->cb_threeState->setTristate(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::RecvGenderID(int id){
    switch (id) {
    case 0:
       qDebug()<<tr("性别：男");
        break;
    case 1:
        qDebug()<<tr("性别：女");
        break;
    default:
        break;
    }
}
void MainWindow::RecvStatusID(int id){
    switch (id) {
    case 0:
       qDebug()<<tr("状态：Good");
        break;
    case 1:
        qDebug()<<tr("状态：Middle");
        break;
    case 2:
        qDebug()<<tr("状态：Bad");
    default:
        break;
    }
}



void MainWindow::on_rb0_19_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("Age :0-19");
    }else{
         qDebug()<<tr("Age is Not :0-19");
    }
}

void MainWindow::on_rb20_39_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("Age :20-39");
    }else{
         qDebug()<<tr("Age is Not :20-39");
    }
}

void MainWindow::on_rb40_59_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("Age :40-59");
    }else{
         qDebug()<<tr("Age is Not :40-59");
    }
}

void MainWindow::on_rb60_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("Age :60 or up");
    }else{
         qDebug()<<tr("Age is less than 60");
    }
}

void MainWindow::on_pushButton_clicked()
{
    QString res;
    int nGenderID = m_GenderGroup->checkedId();
    switch(nGenderID){
    case 0:
        res += tr("性别:男\n");
        break;
    case 1:
        res += tr("性别:女\n");
        break;
    default:
        res += tr("性别未选中\n");
        break;
    }
    int nStatusID = m_StatusGroup->checkedId();
    switch (nStatusID) {
    case 0:
        res += tr("状态：Good\n");
        break;
    case 1:
        res += tr("状态：Middle\n");
        break;
    case 2:
        res += tr("状态：Bad\n");
        break;
    default:
        res+= tr("状态未选中\n");
        break;
    }

    //年龄不是button group 所以需要一个个判断是否按下
    if (ui->rb0_19->isChecked()){
        res += tr("年龄段：0-19\n");
    }else if (ui->rb20_39->isChecked()){
        res += tr("年龄段：20-39\n");
    }else if (ui->rb40_59->isChecked()){
        res += tr("年龄段：40-59\n");
    }else if (ui->rb60->isChecked()){
        res += tr("年龄段：60以上\n");
    }
    QMessageBox::information(this,tr("all info :"),res);
}
//检查6个兴趣爱好复选框，根据选择状态，更新三态复选框显示
void MainWindow::CheckHobbies(){
    int c = 0;
    if (ui->cb_read->isChecked()) c++;
    if (ui->cb_climb->isChecked()) c++;
    if (ui->cb_daze->isChecked()) c++;
    if (ui->cb_eat->isChecked()) c++;
    if (ui->cb_street->isChecked()) c++;
    if (ui->cb_swim->isChecked()) c++;
    if (c <= 0) {
        ui->cb_threeState->setCheckState(Qt::Unchecked);
        return;
    }else if(c <=5){
        ui->cb_threeState->setCheckState(Qt::PartiallyChecked);
    }else {
        ui->cb_threeState->setCheckState(Qt::Checked);
        return;
    }
}

void MainWindow::on_cb_eat_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("eat");
    }
    CheckHobbies();
}

void MainWindow::on_cb_street_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("street");
    }
    CheckHobbies();
}

void MainWindow::on_cb_read_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("read");
    }
    CheckHobbies();
}

void MainWindow::on_cb_swim_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("swim");
    }
    CheckHobbies();
}

void MainWindow::on_cb_climb_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("climb");
    }
    CheckHobbies();
}

void MainWindow::on_cb_daze_toggled(bool checked)
{
    if (checked){
        qDebug()<<tr("daze");
    }
    CheckHobbies();
}

void MainWindow::on_pb_show_clicked()
{
    QString res;

    if (ui->cb_read->isChecked()){
        res += tr("读书 \n");
    }
    if (ui->cb_daze->isChecked()){
        res += tr("发呆\n");
    }
    if (ui->cb_climb->isChecked()){
        res += tr("爬山\n");
    }
    if (ui->cb_eat->isChecked()){
        res += tr("吃饭\n");
    }
    if (ui->cb_street->isChecked()){
        res += tr("逛街\n");
    }
    if (ui->cb_swim->isChecked()){
        res += tr("游泳\n");
    }
    QMessageBox::information(this,tr("兴趣爱好 :"),res);
}

void MainWindow::on_ob_file_clicked()
{
    QDesktopServices::openUrl(QUrl("file:///home/tmacy/"));
}

void MainWindow::on_ob_web_clicked()
{
    QDesktopServices::openUrl(QUrl("https://so.com"));
}
